﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace GeuzenAdministratie
{
    public partial class MemberPresence : Form
    {
        private string KNSA;
        public MemberPresence()
        {
            InitializeComponent();
        }
        public MemberPresence(string KNSA)
        {
            InitializeComponent();
            this.KNSA = KNSA;
            getPresence();
        }
        private void getPresence()
        {
            DBConnect db = new DBConnect();
            DataTable presence = new DataTable();
            presence = db.getPresence(KNSA);
            for (int i = 0; i < presence.Rows.Count; i++) //listbox vullen
            {
                string timestamp = presence.Rows[i]["checkin"].ToString();
                listBox1.Items.Add(timestamp);
            }
            fillMonthCalendar();
            DateTime aanwezig = new DateTime(2001, 6, 10);
        }
        private void fillMonthCalendar() //data bold maken in maandkalender
        {
            monthCalendar1.MaxDate = new System.DateTime(System.DateTime.Now.Year, System.DateTime.Now.Month, System.DateTime.DaysInMonth(System.DateTime.Now.Year, System.DateTime.Now.Month), 23, 59, 59);
            DBConnect db = new DBConnect();
            DataTable presence = new DataTable();
            presence = db.getPresenceMonthCalendar(KNSA);
            for (int i = 0; i < presence.Rows.Count; i++) //listbox vullen
            {
                int year = int.Parse(presence.Rows[i]["year"].ToString());
                int month = int.Parse(presence.Rows[i]["month"].ToString());
                int day = int.Parse(presence.Rows[i]["day"].ToString());
                monthCalendar1.AddBoldedDate(new DateTime(year, month, day)); 
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // step 1: creation of a document-object
            Document myDocument = new Document(PageSize.A4);
            try
            {
                DBConnect db = new DBConnect();
                string name = db.getMemberNameByKNSA(KNSA);
                // step 2:
                // Now create a writer that listens to this doucment and writes the document to desired Stream.
                PdfWriter writer = PdfWriter.GetInstance(myDocument, new FileStream("Aanwezigheid " + name + ".pdf", FileMode.Create));

                // step 3:  Open the document now using
                myDocument.Open();

                // step 4: Now add some contents to the document
                // step 4
                /*
                 * create document header; shows GMT time when PDF created.
                 * HeaderFooter class removed in iText 5.0.0, so we instead write 
                 * content to an **absolute** position on the document
                 */
                iTextSharp.text.Rectangle page = myDocument.PageSize;
                PdfPTable head = new PdfPTable(1);
                head.TotalWidth = page.Width;
                Phrase phrase = new Phrase("Gegenereerd op:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                PdfPCell c = new PdfPCell(phrase);
                iTextSharp.text.Image knsa = iTextSharp.text.Image.GetInstance(GeuzenAdministratie.Properties.Resources.logo_knsa, System.Drawing.Imaging.ImageFormat.Png);
                myDocument.Add(knsa);
                c.Border = iTextSharp.text.Rectangle.NO_BORDER;
                c.VerticalAlignment = Element.ALIGN_TOP;
                c.HorizontalAlignment = Element.ALIGN_CENTER;
                head.AddCell(c);
                head.WriteSelectedRows(
                    // first/last row; -1 writes all rows
                  0, -1,
                    // left offset
                  0,
                    // ** bottom** yPos of the table
                  page.Height - myDocument.TopMargin + head.TotalHeight + 20,
                  writer.DirectContent
                );

                myDocument.Add(new Paragraph("Op onderstaande data was "+name+" aanwezig"+"."));

                myDocument.Add(_stateTable());

            }


            catch (DocumentException de)
            {
                Console.Error.WriteLine(de.Message);
            }
            catch (IOException ioe)
            {
                Console.Error.WriteLine(ioe.Message);
            }

            // step 5: Remember to close the documnet

            myDocument.Close();
        }
        // add a table to the PDF document
        private PdfPTable _stateTable()
        {
            DBConnect db = new DBConnect();
            DataTable presence = db.getPresence(KNSA);
            string[] col = { "Datum en tijdstip van aanmelden" };
            PdfPTable table = new PdfPTable(1);
            /*
            * default table width => 80%
            */
            table.WidthPercentage = 100;
            // then set the column's __relative__ widths
            table.SetWidths(new Single[] { 1 });
            /*
            * by default tables 'collapse' on surrounding elements,
            * so you need to explicitly add spacing
            */
            table.SpacingBefore = 10;

            for (int i = 0; i < col.Length; ++i)
            {
                PdfPCell cell = new PdfPCell(new Phrase(col[i]));
                cell.BackgroundColor = new BaseColor(204, 204, 204);
                table.AddCell(cell);
            }

            // !! database code omitted !!
            // r.Read is the DbDataReader for whatever flavor 
            // of database you're connecting to; we're iterating
            // over the results returned from the database and 
            // adding rows to the table in the PDF 
            
            foreach (DataRow row in presence.Rows) // Loop over the rows.
            {
                table.AddCell(row["checkin"].ToString());

            }

            return table;
        }
    }
}
